/**
 * Created by Jason on 2017/7/16.
 */
/*
 用户数据基本结构定义
 user_id,
 nike_name,
 avatar,
 level,
 type,
 from,
 content,
 action,
 time,
 room_id,

 */
define(['jQuery', 'socket_io'], function (jq, io) {
    var w = {
        room_id: null,
        user_id: null,
        nick_name: null,
        avatar: null,
        level: null,
        type: null,
        from: null,
        socket: null,
        messageType:{
            text:1,
            gift:2,
            notice:3
        },
        showGift:null,
        showMessage:null,
        init: function (room_id, user_id, nick_name, avatar, level, type, from) {
            this.socket = io.connect('http://192.168.1.78:8000?room_id='+room_id);
            this.room_id = room_id;
            this.user_id = user_id;
            this.nick_name = nick_name;
            this.avatar = avatar;
            this.level = level;
            this.type = type;
            this.from = from;

            obj = {
                room_id: room_id,
                user_id: user_id,
                nick_name: nick_name,
                avatar: avatar,
                level: level,
                type: type,
                from: from,
            };
            //登录
            this.socket.emit('login', obj);

            //监听登录
            this.socket.on('login', function (obj) {
                w.updateMessage(obj, 'login');
            });

            //监听收到消息
            this.socket.on('message', function (obj) {
                w.updateMessage(obj, 'message');
            });

            //监听退出信息
            this.socket.on('logout', function (obj) {
                w.updateMessage(obj, 'logout');
            })

        },
        updateMessage: function (obj, action) {
            console.info(obj);
            w.showMessage.call(this,obj,action);
        },
        sendMessage: function (content, type) {
            if (content == '') {
                return;
            }
            var obj = {
                user_id: this.user_id,
                content: content,
                type: type,
            };
            this.socket.emit('message', obj);
        }
    }
    return w;
});